package com.haiyou.common.util.date;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import com.haiyou.common.util.time.ServerTime;


public class TimeHelper {

	/**
	 * 每天5点重置
	 */
	public static final int EVERY_RESET_TIME = 5;
	/**
	 * 一天时长（ms）
	 */
	public static final long DAY = 24l * 60 * 60 * 1000;
	/**
	 * 一小时（ms)
	 */
	public static final long HOUR = 60l * 60 * 1000;
	/**
	 * 一分钟（ms）
	 */
	public static final long MINUTE = 60l * 1000;

	/**
	 * 将一个累积时长转换为 天 00:00:00
	 * 
	 * @param time 累积毫秒数
	 */
	public static String formatTime(long time) {
		long day = time / DateUtilsV2.DAY_MILLISECOND;
		long remainH = time % DateUtilsV2.DAY_MILLISECOND;
		long hour = remainH / DateUtilsV2.HOUR_MILLISECOND;
		long remainM = remainH % DateUtilsV2.HOUR_MILLISECOND;
		long minute = remainM / DateUtilsV2.MINUTE_MILLISECOND;
		long remainS = remainM % DateUtilsV2.MINUTE_MILLISECOND;
		long second = remainS / DateUtilsV2.SECOND_MILLISECOND;
		String sDay = day > 0 ? String.valueOf(day) : "";
		String sHour = hour < 10 ? ("0" + hour) : ("" + hour);
		String sMinute = minute < 10 ? ("0" + minute) : ("" + minute);
		String sSecond = second < 10 ? ("0" + second) : ("" + second);
		return sDay + " " + sHour + ":" + sMinute + ":" + sSecond;
	}

	/**
	 * 今天是否已经重置了数据.
	 */
	public static boolean hasTodayReset(long lastResetMilli) {
		long now = ServerTime.timeMillis();
		long lastDayResetMilliTime = getLastDayResetMilliTime();
		// 昨天重置的
		if (lastResetMilli < lastDayResetMilliTime) {
			return false;
		}
		long everyDayResetTime = getTodayResetMilliTime();
		// 不到重置时间
		if (now < everyDayResetTime) {
			return true;
		}
		return lastResetMilli >= everyDayResetTime;
	}

	/**
	 * 返回上一天的重置时间.
	 */
	public static long getLastDayResetMilliTime() {
		return getTodayResetMilliTime() - (86400 * 1000);
	}

	/**
	 * 当日的每日重置时间.
	 */
	public static long getTodayResetMilliTime() {
		return DateUtilsV2.getTodayZeroMillis() + TimeUnit.HOURS.toMillis(EVERY_RESET_TIME);
	}

	/**
	 * 指定日期的每日重置时间
	 */
	public static long getDateResetMilliTime(long date) {
		return DateUtilsV2.getDayZeroMillis(date) + TimeUnit.HOURS.toMillis(EVERY_RESET_TIME);
	}


	/**
	 * 获取当日feed点赞次数的开始时间戳 ms
	 */
	public static long getFeedStartTime() {
		int hour = DateUtilsV2.getHour();
		if (hour < EVERY_RESET_TIME) {
			return getLastDayResetMilliTime();
		}
		return getTodayResetMilliTime();
	}

	/**
	 * 当日凌晨5点.
	 */
	public static long getTodayFiveAMMilli() {
		return DateUtilsV2.getTodayZeroMillis() + 5 * 3600 * 1000;
	}

	/**
	 * 当前凌晨4点.
	 */
	public static long getTodayFourAMMilli() {
		return DateUtilsV2.getTodayZeroMillis() + 4 * 3600 * 1000;
	}

	/**
	 * 获取这个礼拜一的零点时间.
	 */
	public static long getThisWeekMonDayZeroMillis() {
		Date date = getThisWeekMonday();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTimeInMillis();
	}

	/**
	 * 获取这个礼拜一的四点时间.
	 */
	public static long getThisWeekMonDayFourAmMillis() {
		return getThisWeekMonDayZeroMillis() + (4 * 3600) * 1000;
	}

	/**
	 * 获取这个礼拜一的五点时间.
	 */
	public static long getThisWeekMonDayFiveAmMillis() {
		return getThisWeekMonDayZeroMillis() + (5 * 3600) * 1000;
	}

	/**
	 * 获取下个礼拜一的四点.
	 */
	public static long getNextWeekMonDayFourAMMillis() {
		return getThisWeekMonDayZeroMillis() + (86400 * 7 + 4 * 3600) * 1000;
	}

	/**
	 * 获取下个礼拜一的五点.
	 */
	public static long getNextWeekMonDayFiveAMMillis() {
		return getThisWeekMonDayZeroMillis() + (86400 * 7 + 5 * 3600) * 1000;
	}

	/**
	 * 获取上个礼拜一
	 */
	public static Date geLastWeekMonday() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getThisWeekMonday());
		cal.add(Calendar.DATE, -7);
		return cal.getTime();
	}

	/**
	 * 获取这周的星期几的几点时间.
	 * 
	 * @param weekWay 1 是星期六
	 * @param hour    24小时制
	 */
	public static long getThisWeekDayHourInMillis(int weekWay, int hour) {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(weekWay);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTimeInMillis();
	}

	/**
	 * 获取这个礼拜一
	 */
	public static Date getThisWeekMonday() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 获得当前日期是一个星期的第几天
		int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
		if (1 == dayWeek) {
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		// 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		// 获得当前日期是一个星期的第几天
		int day = cal.get(Calendar.DAY_OF_WEEK);
		// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
		cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
		return cal.getTime();
	}

	/**
	 * 返回离当前时间最近的过去整点时间
	 */
	public static long getLastHourInMilli() {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.HOUR, 1);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return ca.getTimeInMillis() - 3600 * 1000;
	}

	/**
	 * 返回下个整点时间
	 */
	public static long getNextHourInMilli() {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.HOUR, 1);
		ca.set(Calendar.MINUTE, 0);
		ca.set(Calendar.SECOND, 0);
		ca.set(Calendar.MILLISECOND, 0);
		return ca.getTimeInMillis();
	}
}
